<?php
namespace Common\Model;

use Think\Model;

class MenuModel extends  CommonModel {

	public $tableName = 'menu';
	//状态
	public $status = [
		1 => '启用',
		2 => '禁用',
	];

	//查询渠道列表
	public function get_menu_list(array $params,$field="a.*"){
		$where = [];
		$pagesize = empty($params['pageSize']) ? '20' : $params['pageSize'];
		$params["menu_name"]?$where['a.menu_name'] = ["LIKE","%{$params['menu_name']}%"]:"";
		$params['status']?$where['a.status'] = $params['status']:"";
		$count = $this
			->alias("a")
			->where($where)
			->count();
		$Page = new \Think\Page($count, $pagesize);
		$limit = $Page->firstRow . ',' . $Page->listRows;

		$list = $this
			->alias("a")
			->field($field)
			->where($where)
			->order($params['order']?$params['order']:"a.id desc")
			->limit($limit)
			->select();
		int_to_string($list,['status'=>$this->status]);
		$data = [
			'count' => $count,
			'list'  => $list
		];
		return $data;
	}




	//添加/修改渠道
	public function menu_save(array $params){
		$where = [
			'menu_name' => $params['menu_name'],
		];
		if($params['menu_id']){
			$where['id'] = ['neq',$params['menu_id']];
		}
		$info = $this->where($where)->find();
		if($info) return $this->returnData("套餐名称已存在,请重新输入");
		$img = "";
		if($params['image'][0]['url']){
			$imgs = explode("Uploads",$params['image'][0]['url']);
			$img = "/Uploads".$imgs[1];
		}
		$data = [
			'menu_name'   => $params['menu_name'],
			'image'       => $img,
			'status'      => $params['status'],
			'price' 	  => $params['price'],
			'internet_traffic' => $params['internet_traffic'],
			'broadband'	 => $params['broadband'],
			'content'    => $params['content'],
			'appid'		 => $params['appid'],
			'url'		 => $params['url']
		];


		if ($params['menu_id']) {
			$data['update_time'] = time();
			$data['update_by'] = $params['userId'];
			$result = $this->where(['id' => $params['menu_id']])->save($data);
		} else {
			$data['create_time'] = time();
			$data['create_by'] = $params['userId'];
			$result = $this->add($data);
		}
		return $this->returnData($result?"设置成功":"设置失败",$result?200:500);
	}




}

?>
